// This is autogenerated file. Do not edit!

package models.autogenerated;

class AuthManager
{
	var db : orm.Db;
	var orm : models.Orm;
	public var query(get, never) : orm.SqlQuery<models.Auth>;

	function get_query() : orm.SqlQuery<models.Auth>
	{
		return new orm.SqlQuery<models.Auth>("auth", db, this);
	}

	public function new(db:orm.Db, orm:models.Orm) : Void
	{
		this.db = db;
		this.orm = orm;
	}

	function newModelFromParams(privkey:String, devicekey:String, user_id:String, last_use:String) : models.Auth
	{
		var _obj = new models.Auth(db, orm);
		_obj.privkey = privkey;
		_obj.devicekey = devicekey;
		_obj.user_id = user_id;
		_obj.last_use = last_use;
		return _obj;
	}

	function newModelFromRow(d:Dynamic) : models.Auth
	{
		var _obj = new models.Auth(db, orm);
		_obj.privkey = Reflect.field(d, 'privkey');
		_obj.devicekey = Reflect.field(d, 'devicekey');
		_obj.user_id = Reflect.field(d, 'user_id');
		_obj.last_use = Reflect.field(d, 'last_use');
		return _obj;
	}

	public function where(field:String, op:String, value:Dynamic) : orm.SqlQuery<models.Auth>
	{
		return query.where(field, op, value);
	}

	public function get(privkey:String) : models.Auth
	{
		return getBySqlOne('SELECT * FROM `auth` WHERE `privkey` = ' + db.quote(privkey));
	}

	public function create(privkey:String, devicekey:String, user_id:String, last_use:String) : models.Auth
	{
		db.query('INSERT INTO `auth`(`privkey`, `devicekey`, `user_id`, `last_use`) VALUES (' + db.quote(privkey) + ', ' + db.quote(devicekey) + ', ' + db.quote(user_id) + ', ' + db.quote(last_use) + ')');
		return newModelFromParams(privkey, devicekey, user_id, last_use);
	}

	public function createNamed(data:{ privkey:String, devicekey:String, user_id:String, last_use:String }) : models.Auth
	{
		db.query('INSERT INTO `auth`(`privkey`, `devicekey`, `user_id`, `last_use`) VALUES (' + db.quote(data.privkey) + ', ' + db.quote(data.devicekey) + ', ' + db.quote(data.user_id) + ', ' + db.quote(data.last_use) + ')');
		return newModelFromParams(data.privkey, data.devicekey, data.user_id, data.last_use);
	}

	public function createOptional(data:{ privkey:String, ?devicekey:String, ?user_id:String, ?last_use:String }) : models.Auth
	{
		createOptionalNoReturn(data);
		return get(data.privkey);
	}

	public function createOptionalNoReturn(data:{ privkey:String, ?devicekey:String, ?user_id:String, ?last_use:String }) : Void
	{
		var fields = [];
		var values = [];
		fields.push('`privkey`'); values.push(db.quote(data.privkey));
		if (Reflect.hasField(data, 'devicekey')) { fields.push('`devicekey`'); values.push(db.quote(data.devicekey)); }
		if (Reflect.hasField(data, 'user_id')) { fields.push('`user_id`'); values.push(db.quote(data.user_id)); }
		if (Reflect.hasField(data, 'last_use')) { fields.push('`last_use`'); values.push(db.quote(data.last_use)); }
		db.query('INSERT INTO `auth`(' + fields.join(", ") + ') VALUES (' + values.join(", ") + ')');
	}

	public function delete(privkey:String) : Void
	{
		db.query('DELETE FROM `auth` WHERE `privkey` = ' + db.quote(privkey) + ' LIMIT 1');
	}

	public function getAll(_order:String=null) : Array<models.Auth>
	{
		return getBySqlMany('SELECT * FROM `auth`' + (_order != null ? ' ORDER BY ' + _order : ''));
	}

	public function getBySqlOne(sql:String) : models.Auth
	{
		var rows = db.query(sql + ' LIMIT 1');
		if (rows.length == 0) return null;
		return newModelFromRow(rows.next());
	}

	public function getBySqlMany(sql:String) : Array<models.Auth>
	{
		var rows = db.query(sql);
		var list : Array<models.Auth> = [];
		for (row in rows)
		{
			list.push(newModelFromRow(row));
		}
		return list;
	}
}